<?php

namespace catchAdmin\model\wechat;

use EasyWeChat\Factory;
use think\facade\Config;

class PaymentApp extends \EasyWeChat\Payment\Application
{

    private $app_id = '';
    private $mch_id = '';
    private $key    = '';
    private $app    = null;


    public function __construct($app_id)
    {
        $config = [
            // 必要配置
//            'app_id'    => Config::get('wechat.MiniProgramApp.app_id'),
            'app_id'    => $app_id,
            'mch_id'    => Config::get('wechat.PaymentApp.mch_id'),
            'key'       => Config::get('wechat.PaymentApp.key'),   // API v2 密钥 (注意: 是v2密钥 是v2密钥 是v2密钥)

            // 如需使用敏感接口（如退款、发送红包等）需要配置 API 证书路径(登录商户平台下载 API 证书)
            'cert_path' => '', // XXX: 绝对路径！！！！
            'key_path'  => '',      // XXX: 绝对路径！！！！

            'notify_url' => '',     // 你也可以在下单时单独设置来想覆盖它
        ];

        $this->app = Factory::payment($config);

        return $this->app;
    }


    public function payInit($param)
    {
        saveAccessLog($param, 'payInit');

        $pay_pre = $this->app->order->unify([
            'body'         => $param['body'],
            'out_trade_no' => $param['out_trade_no'],
            'total_fee'    => $param['total_fee'] * 100,
            'notify_url'   => request()->domain() . $param['notify_url'], // 支付结果通知网址，如果不设置则会使用配置里的默认地址
            'trade_type'   => $param['trade_type'], // 请对应换成你的支付方式对应的值类型
            'openid'       => $param['openid'],
        ]);

        $msg = $this->app->jssdk->sdkConfig($pay_pre['prepay_id']);

        return $msg;
    }


}
